package com.rh.controller;
import com.rh.entity.EduCourse;
import com.rh.entity.vo.CoursePageVo;
import com.rh.entity.vo.CoursePublishVo;
import com.rh.entity.vo.CourseVo;
import com.rh.service.EduCourseService;
import com.rh.result.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程 前端控制器
 */
@RestController
@RequestMapping("/eduservice/course")
@Api(tags = "课程管理")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    /**
     * 添加课程基本信息
     */
    @ApiOperation(value = "1.新增课程")
    @PostMapping("addCourseInfo")
    public ResultVo addCourseInfo(@RequestBody CourseVo courseVo){
        String id=eduCourseService.saveCourseInfo(courseVo);
        //为了前端添加课程大纲能返回,这里得传一个id
        return ResultVo.ok().data("courseId",id);
    }
    /**
     * 根据课程ID查询章节信息
     * 用于点击上一步课程信息回显
     */
    @ApiOperation(value = "2.根据课程ID查询课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public ResultVo getCourseInfo(@PathVariable String courseId){
        CourseVo courseInfo=eduCourseService.getCourseInfo(courseId);
        return ResultVo.ok().data("courseInfo",courseInfo);
    }
    /**
     * 修改课程信息
     */
    @ApiOperation(value = "3.修改课程信息")
    @PostMapping("updateCourseInfo")
    public ResultVo updateCourseInfo(@RequestBody CourseVo courseVo){
        eduCourseService.updateCouseInfo(courseVo);
        return ResultVo.ok();
    }
    /**
     * 根据课程id查询课程的所有基本信息(联查)
     */
    @ApiOperation(value = "4.根据课程id查询课程的所有基本信息(联查)")
    @GetMapping("getPublishCourseInfo/{id}")
    public ResultVo getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo= eduCourseService.getPublishCourse(id);
        return ResultVo.ok().data("publishCourse",coursePublishVo);
    }
    /**
     * 课程最终发布
     * 修改课程的状态
     */
    @ApiOperation(value = "5.课程最终发布")
    @PostMapping("publishCourse/{id}")
    public ResultVo publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        //设置课程id
        eduCourse.setId(id);
        //设置课程发布状态
        eduCourse.setStatus("Normal");
        boolean b = eduCourseService.updateById(eduCourse);
        if(b){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    /**
     * 条件查询带分页
     */
    @ApiOperation(value = "6.课程列表分页查询带条件")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public ResultVo getCourseList(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) CoursePageVo coursePageVo){
        return eduCourseService.pageQuery(current,limit, coursePageVo);
    }
    /**
     * 分页查询
     */
    @ApiOperation(value = "7.分页查询课程信息")
    @GetMapping("coursePage/{current}/{limit}")
    public ResultVo findCoursePage(@PathVariable Long current,
                                   @PathVariable Long limit){
        return eduCourseService.findCoursePage(current,limit);
    }
    /**
     * 删除课程信息
     */
    @ApiOperation(value = "8.删除课程信息")
    @DeleteMapping("{courseId}")
    public ResultVo deleteCourse(@ApiParam(name = "courseId",value = "课程ID",required = true) @PathVariable String courseId) {
       eduCourseService.removeCourse(courseId);
       return ResultVo.ok();
    }
}

